package parser;

import antlr.RecognitionException;
import java.util.*;

class AssertStmt extends Stmt {
    
    Stmt body;
    
    AssertStmt(Expr cond, Expr message) {
        if (message == null)
            message = Expr.NONE;
        Stmt raise = new RaiseStmt(new AssertExpr(message));
        body = new IfStmt(new NotExpr(cond), raise);
    }
 
    
    
    void write(ByteStream out) throws RecognitionException {
        out.line(line);
        out.write(Opcodes.DEBUG);
        Patch end = out.branch(Opcodes.ON_FALSE);
        body.write(out);
        end.patch(out);
    }
    
}

class AssertExpr extends Expr {
    
    AssertExpr(Expr message)  {
        this.message = message;
    }
    
    Expr message;
    
    void delete(ByteStream out) throws RecognitionException {
        throw new RecognitionException("Cannot delete rvalue");
    }
    
    void write(ByteStream out) throws RecognitionException {
        out.write(Opcodes.LOAD_GLOBAL);
        out.writeShort(StandardNames.AssertionError);
        message.write(out);
        out.write(Opcodes.PACK_PARAMS);
        out.write(1);
        out.write(Opcodes.F_CALL);
    }
    
    void writeLHS(ByteStream out) throws RecognitionException {
        throw new IllegalStateException();   
    }
    
}
